package cn.yonyong.base.project.service.impl;

import cn.yonyong.base.project.entity.TblUserappear;
import cn.yonyong.base.project.mapper.TblUserappearMapper;
import cn.yonyong.base.project.service.UserAppearService;
import cn.yonyong.base.project.utils.PoiExcelExport;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Describtion Todo
 * @Author yonyong
 * @Date 2019/9/6 17:07
 * @Version 1.0.0
 **/
@Service
@Transactional(readOnly = true)
public class UserAppearServiceImpl implements UserAppearService {

    @Resource
    private TblUserappearMapper tblUserappearMapper;

    @Override
    public JSONObject exportExcel(HttpServletRequest request, HttpServletResponse response) {
        Map<String,Object> resultMap = new HashMap<>();
        List<TblUserappear> list = new ArrayList<>();

        //导出10条记录
        list = tblUserappearMapper.selectByRowBounds(null,new RowBounds(0,10));

        //过滤出需要导出的数据信息，与keyList 对应
        List<Map<String, Object>> exportList = new ArrayList<>();
        exportList = getExportLists(list);

        String[] temp_cellname = { "id", "批次", "姓名", "状态", "时间", "AAA", "PKI","MDM" };
        String[] temp_keyList = { "id", "Batch", "name", "State", "Time", "AAA", "PKI","MDM" };
        String export_file_name = "批次明细报表";

        //开始导出业务
        try {
            response.setContentType("application/vnd.ms-excel");
            // 根据浏览器类型处理文件名称
            String agent = request.getHeader("USER-AGENT").toLowerCase();
            if (agent.indexOf("firefox") > -1) {// 若是火狐
                export_file_name = new String(export_file_name.getBytes("UTF-8"), "ISO8859-1");
            } else {// 其他浏览器
                export_file_name = java.net.URLEncoder.encode(export_file_name, "UTF-8");
            }
            OutputStream out = response.getOutputStream();
            // 保存导出的excel的名称
            response.setHeader("Content-Disposition", "attachment;filename=" + export_file_name + ".xls");
            PoiExcelExport.exportExcelExceptBianHao(export_file_name, temp_cellname, exportList, temp_keyList, out);
            out.flush();
            out.close();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        resultMap.put("result","导出报表完成");
        return (JSONObject) JSONObject.toJSON(resultMap);
    }

    private List<Map<String,Object>> getExportLists(List<TblUserappear> list) {
        List<Map<String,Object>> result_list = new ArrayList<>();
        for (TblUserappear tblUserappear : list){
            Map<String,Object> temp_map = new HashMap<>();
            temp_map.put("id",tblUserappear.getId());
            temp_map.put("Batch",tblUserappear.getBatch());
            temp_map.put("name",tblUserappear.getName());
            temp_map.put("State",tblUserappear.getState());
            temp_map.put("Time",tblUserappear.getTime());
            temp_map.put("AAA",tblUserappear.getAaa());
            temp_map.put("PKI",tblUserappear.getPki());
            temp_map.put("MDM",tblUserappear.getMdm());
            result_list.add(temp_map);
        }
        return result_list;
    }
}
